<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <button v-on:click="addClick">点击+1</button>
        <p>{{this.$store.state.count}}</p>
    </div>
</body>
<script src="./node_modules/vue/dist/vue.js"></script>
<script src="./node_modules/vuex/dist/vuex.js"></script>
<script>

    let store = new Vuex.Store({
        //仓库，相当于new Vue中的data
        state:{
            count:123,
        },
        //改变值的操作，相当于new Vue中的methods
        mutations:{
            addNum:function(state,num){
                state.count+=num;
            },
        },
        //actions可异步获取数据，
        actions:{
            getNum:function(store){
                //可发送请求获取数据，
                setTimeout(()=>{  //模拟请求
                    store.commit("addNum",10);
                },1000)
            }
        }
    });

    let vm = new Vue({
        el:"#app",
        store,
        methods:{
            addClick:function(){
                // this.$store.commit("addNum");   //commit 调动mutations中的方法
                this.$store.dispatch("getNum");    //dispatch 调动actions中的方法
            }
        }
    })
</script>
</html>